import { Toast, Dialog } from 'vant'
import { getPlatform } from '@/utils'

const install = function(Vue) {

  const Vgo = new Vue({

    methods: {
      emit(event, ...args) {
        this.$emit(event, ...args)
      },
      on(event, cb) {
        this.$on(event, cb)
      },
      off(event, cb) {
        this.$off(event, cb)
      },
      once(event, ...args) {
        this.$emit(event, ...args)
        this.$off(event)
      },
      tip(msg, type, time, ctrl) {
        // 根据设备对应提示类型
        if(getPlatform()) {
          this.$message({
            message: msg || '操作成功！',
            showClose: ctrl || false,
            duration: time || 1000,
            type: type || 'info'
          })
        } else {
          if(type != 'success') {
            type = 'fail'
          }
          Toast[type]({
            message: msg,
            duration: time || 1000
          })
        }
      },
      open (cb, message = '是否确认当前操作?', opts = {}) {
        const { cancelCb = () => {} } = opts
        Dialog.confirm({
          title: '提示',
          message,
          closeOnClickOverlay: true,
        }).then(cb).catch(cancelCb)
      },
      notify(title, msg) {
        this.$notify.info({
          title: title || '提示',
          message: msg || '工程师正在开发这个页面'
        })
      },
      delay(time, cb) {
        setTimeout(cb ,time || 1000)
      },
      openLoading (message = 'Loading...') {
        if (this.loading) return
        this.loading = true
        this.toast2 = Toast.loading({
          duration: 0,
          forbidClick: true,
          message,
        })
      },
      closeLoading () {
        this.loading = false
        this.toast2.clear()
      },
    }
  })
  Vue.prototype.$vgo = Vgo
}

export default install
